package com.ytjj.qmyx.admin.controller;

import com.ytjj.common.api.CommonPage;
import com.ytjj.common.api.CommonResult;
import com.ytjj.qmyx.admin.annotation.OperLog;
import com.ytjj.qmyx.admin.constants.SysLogConstants;
import com.ytjj.qmyx.admin.model.request.ProductAttributeRequest;
import com.ytjj.qmyx.admin.service.ProductAttributeService;
import com.ytjj.qmyx.mall.model.ProductAttributeKey;
import com.ytjj.qmyx.mall.model.ProductAttributeValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品属性管理
 */
@RestController
@RequestMapping("/productAttribute")
public class ProductAttributeController {

    @Autowired
    private ProductAttributeService productAttributeService;

    /**
     * 查询商品属性key列表
     * @param productAttributeRequest
     * @return
     */
    @PostMapping("/keyList")
    public CommonResult keyList(@RequestBody ProductAttributeRequest productAttributeRequest){
        CommonPage<ProductAttributeKey> pageList = productAttributeService.keyList(productAttributeRequest);
        return CommonResult.success(pageList);
    }

    /**
     * 查询商品属性key全部数据
     * @return
     */
    @GetMapping("/keysAll")
    public CommonResult keysAll(){
        List<ProductAttributeKey> keysList = productAttributeService.keysAll();
        return CommonResult.success(keysList);
    }

    /**
     * 新增商品属性key信息
     * @param productAttributeKey
     * @return
     */
    @PostMapping("/createKey")
    @OperLog(operModule = "商品属性模块", operDesc = "新增商品属性key信息", operType = SysLogConstants.OPERATION_ADD)
    public CommonResult createKey(@RequestBody ProductAttributeKey productAttributeKey){
        int count = productAttributeService.createKey(productAttributeKey);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    /**
     * 修改商品属性key信息
     * @param productAttributeKey
     * @return
     */
    @PostMapping("/updateKey")
    @OperLog(operModule = "商品属性模块", operDesc = "修改商品属性key信息", operType = SysLogConstants.OPERATION_UPDATE)
    public CommonResult updateKey(@RequestBody ProductAttributeKey productAttributeKey){
        int count = productAttributeService.updateKey(productAttributeKey);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    /**
     * 根据id获取属性key信息
     * @param id
     * @return
     */
    @GetMapping("/getKey")
    public CommonResult<ProductAttributeKey> getKey(@RequestParam Integer id){
        ProductAttributeKey productAttributeKey = productAttributeService.getKey(id);
        return CommonResult.success(productAttributeKey);
    }

    /**
     * 根据主键删除
     * @param id
     * @return
     */
    @PostMapping("/deleteKey")
    @OperLog(operModule = "商品属性模块", operDesc = "根据主键删除", operType = SysLogConstants.OPERATION_DELETE)
    public CommonResult deleteKey(@RequestParam Integer id){
        int count = productAttributeService.deleteKey(id);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    // ------------------------------------------------------------ 分界线 ------------------------------------------------------------------

    /**
     * 查询商品属性value列表
     * @param productAttributeRequest
     * @return
     */
    @PostMapping("/valueList")
    public CommonResult valueList(@RequestBody ProductAttributeRequest productAttributeRequest){
        CommonPage<ProductAttributeValue> pageList = productAttributeService.valueList(productAttributeRequest);
        return CommonResult.success(pageList);
    }

    /**
     * 查询商品属性value全部数据
     * @return
     */
    @GetMapping("/valuesAll")
    public CommonResult valuesAll(){
        List<ProductAttributeValue> valuesList = productAttributeService.valuesAll();
        return CommonResult.success(valuesList);
    }


    /**
     * 新增商品属性value信息
     * @param productAttributeValue
     * @return
     */
    @PostMapping("/createValue")
    @OperLog(operModule = "商品属性模块", operDesc = "新增商品属性value信息", operType = SysLogConstants.OPERATION_ADD)
    public CommonResult createValue(@RequestBody ProductAttributeValue productAttributeValue){
        int count = productAttributeService.createValue(productAttributeValue);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    /**
     * 修改商品属性value信息
     * @param productAttributeValue
     * @return
     */
    @PostMapping("/updateValue")
    @OperLog(operModule = "商品属性模块", operDesc = "修改商品属性value信息", operType = SysLogConstants.OPERATION_UPDATE)
    public CommonResult updateValue(@RequestBody ProductAttributeValue productAttributeValue){
        int count = productAttributeService.updateValue(productAttributeValue);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    /**
     * 根据id获取属性value信息
     * @param id
     * @return
     */
    @GetMapping("/getValue")
    public CommonResult<ProductAttributeValue> getValue(@RequestParam Integer id){
        ProductAttributeValue productAttributeValue = productAttributeService.getValue(id);
        return CommonResult.success(productAttributeValue);
    }

    /**
     * 根据主键删除
     * @param id
     * @return
     */
    @PostMapping("/deleteValue")
    @OperLog(operModule = "商品属性模块", operDesc = "根据主键删除", operType = SysLogConstants.OPERATION_DELETE)
    public CommonResult deleteValue(@RequestParam Integer id){
        int count = productAttributeService.deleteValue(id);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }


    /**
     * 根据key获取属性value信息
     * @param key
     * @return
     */
    @GetMapping("/getValueBykey")
    public CommonResult<List<ProductAttributeValue>> getValueBykey(@RequestParam Integer key){
        List<ProductAttributeValue> productAttributeValues = productAttributeService.getValueBykey(key);
        return CommonResult.success(productAttributeValues);
    }



}
